/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.gac.utils;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import org.gac.genetics.FitnessFunction;
import org.openide.util.Exceptions;

/**
 *
 * @author Tomek
 */
public class JarLoader
{

    private static final Class[] parameters = new Class[]
    {
        URL.class
    };

    public static void addFile(String s)
    {
        File f = new File(s);
        if( !f.exists())
            return;
        addFile(f);
    }//end method

    public static void addFile(File f)
    {
        try
        {
            addURL(f.toURL());
        } catch (MalformedURLException ex)
        {
            Exceptions.printStackTrace(ex);
        }
    }//end method

    public static void addURL(URL u)
    {

        URLClassLoader sysloader = (URLClassLoader)ClassLoader.
                getSystemClassLoader();
        Class sysclass = URLClassLoader.class;

        try
        {
            Method method = sysclass.getDeclaredMethod("addURL", parameters);
            method.setAccessible(true);
            method.invoke(sysloader, new Object[]
                    {
                        u
                    });
        } catch (Throwable t)
        {
            t.printStackTrace();
            //throw new IOException(
            //        "Error, could not add URL to system classloader");
        }//end try catch

    }//end method
}
